package com.example.nbastar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.nbastar.bean.User;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG= "LoginActivity";
    EditText mEtName,mEtPwd;
    TextView mTvReg;
    Button mBtnLogin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }
    private void init() {
        mEtName = findViewById(R.id.user);
        mEtPwd = findViewById(R.id.password);
        mTvReg = findViewById(R.id.zhuce);
        mTvReg.setOnClickListener(this);
        mBtnLogin = findViewById(R.id.denglu);
        mBtnLogin.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.denglu:
                final String name = mEtName.getText().toString().trim();
                String password = mEtPwd.getText().toString().trim();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "请输入完整", Toast.LENGTH_SHORT).show();
                    return;
                }
                OkHttpClient client = new OkHttpClient();
                RequestBody body = new FormBody.Builder()
                        .add("name", name)
                        .add("password", password)
                        .build();
                final Request request = new Request.Builder()
                        .url("http://192.168.43.21:8080/nbaserver/user/login")
                        .post(body)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {

                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        Log.e(TAG, "onFailure: ");
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String jsonStr = response.body().string();
                        Log.e(TAG, "onResponse: " + jsonStr);
                        Gson gson = new Gson();
                        User user = gson.fromJson(jsonStr, User.class);
                      /*  User user=new User();*/
                        if (user.isSuccess()) {
                            SharedPreferences info = getSharedPreferences("User", MODE_PRIVATE);
                            SharedPreferences.Editor edit = info.edit();
                            edit.putString("name", user.getData().getName());
                            edit.putString("password", user.getData().getPassword());
                            edit.putInt("id", user.getData().getId());
                            edit.commit();
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));

                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this, "输入的用户名或密码错误", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                });
                break;
            case R.id.zhuce:
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivity(intent);
                break;
        }
    }
}

